<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="/public/header-layui::head">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <table id="currentTableId" class="layui-table" lay-filter="currentTableIdFilter"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
    </div>
</script>

<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form','table', 'treeTable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treeTable = layui.treeTable;
        var form = layui.form;

        // 渲染表格
        treeTable.render({
            elem: '#currentTableId',
            url: '/menu/jsonList',
            //height: 'full-200',
            tree: {
                iconIndex: 1,
                isPidData: true,
                idName: 'menuId',
                pidName: 'parentId'
            },
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter','exports', 'print'],
            cols: [
                [
                    //{type: 'numbers'},
                    {type: 'checkbox'},
                    {field: 'menuName', minWidth: 200, title: '权限名称'},
                    {field: 'menuAuthority', title: '权限标识'},
                    {field: 'menuLink', title: '菜单url'},
                    {field: 'orderNumber', width: 80, align: 'center', title: '排序号'},
                    {
                        field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                            if (d.isMenu == 1) {
                                return '<span class="layui-badge layui-bg-gray">按钮</span>';
                            }
                            if (d.parentId == -1) {
                                return '<span class="layui-badge layui-bg-blue">目录</span>';
                            } else {
                                return '<span class="layui-badge-rim">菜单</span>';
                            }
                        }, title: '类型'
                    },
                    {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
                ]
            ],
            style: 'margin-top:0;'
        });

        // 工具列点击事件
        treeTable.on('tool(currentTableId)', function (obj) {
            var event = obj.event;

            if (event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        method: "GET",
                        url: "/menu/deleteMenu?id="+obj.data.menuId,
                        async: true,
                        dataType: "json",
                        success: function(data) {

                            if(data.code == 1) {
                                layer.msg(data.data);
                                //删除json数据
                                obj.del();
                            } else {
                                layer.msg(data.data);
                            }
                            //关闭询问层
                            layer.close(index);
                        },
                        error: function(e) {
                            layer.alert(e);
                        }
                    });
                });

            } else if (event === 'edit') {
                var index = layer.open({
                    title: '编辑角色',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['90%', '90%'],
                    content: '/menu/menuEdit?id='+obj.data.menuId,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
        });

        //新增
        treeTable.on('toolbar(currentTableId)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '新增菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['90%', '90%'],
                    content: '/menu/menuAdd',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            /*
            else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }

             */
        });

    });
</script>
    <style>
        .ew-tree-table .ew-tree-table-tool{padding-bottom: 0}
    </style>
</body>
</html>